<template>
  <div class="app-container" style="height: 100%">
    <el-card>
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="客户姓名">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入客户姓名"
            clearable
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item label="状态">
          <el-select
            v-model="queryParams.status"
            clearable
            placeholder="请选择状态"
            style="width: 240px"
          >
            <el-option
              v-for="dict in agent_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search"
@click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain
icon="Plus" @click="add"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="postList"
        stripe
        border
        max-height="500"
      >
        <el-table-column label="客户姓名" align="center" prop="nickName" />
        <el-table-column label="性别" align="center">
          <template #default="scope">
            <span>
              {{
                sys_user_sex.find((item) => item.value == scope.row.sex)
                  ? sys_user_sex.find((item) => item.value == scope.row.sex)
                      .label
                  : ""
              }}
            </span>
          </template>
        </el-table-column>

        <el-table-column label="手机号" align="center" prop="phone" />

        <el-table-column label="状态" align="center">
          <template #default="scope">
            <span>
              {{
                agent_status.find((item) => item.value == scope.row.status)
                  ? agent_status.find((item) => item.value == scope.row.status)
                      .label
                  : ""
              }}
            </span>
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <el-button link type="primary"
@click="stopUse(scope.row)">{{
              scope.row.status == "1" ? "启用" : "停用"
            }}</el-button>
            <!--            <el-button link-->
            <!--type="primary" @click="edit(scope.row)"-->
            <!--              >编辑</el-button-->
            <!--            >-->
            <el-button link type="primary"
@click="del(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.limit"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      v-model="open"
      :title="title"
      append-to-body
      width="600px"
      v-if="open"
      @close="cancel"
      :close-on-click-modal="false"
    >
      <el-form
        ref="CustomerServiceAgentsRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="客服姓名" prop="userId">
          <!--          <el-input v-model="form.name" placeholder="请输入客服姓名" />-->

          <el-select
            v-model="form.userId"
            clearable
            placeholder="请选择客服姓名"
            style="width: 100%"
            @change="changePeople"
          >
            <el-option
              v-for="dict in sysPepoleList"
              :key="dict.userId"
              :label="dict.nickName"
              :value="dict.userId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select
            v-model="form.sex"
            clearable
            placeholder="请选择性别"
            style="width: 100%"
          >
            <el-option
              v-for="dict in sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="(item, index) in agent_status"
              :key="index"
              :label="item.value"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitFormDebounceHandler"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改岗位对话框 -->
  </div>
</template>

<script setup name="CustomerServiceAgents">
const { proxy } = getCurrentInstance();
const { sys_normal_disable, sys_user_sex, agent_status } = proxy.useDict(
  "sys_normal_disable",
  "sys_user_sex",
  "agent_status",
);
const router = useRouter();

import { debounce } from "@/utils/debounce.js";
import {
  agentDelete,
  agentList,
  agentSave,
  isEnableStatus,
  getSysUserSelectOptions,
} from '@/api/smartSpeaker/index';

const showSearch = ref(true);
const loading = ref(true);
const postList = ref([]);
const open = ref(false);
const title = ref("新增客服人员");
const sysPepoleList = ref([])
const total = ref(0);

const data = reactive({
  form: {
    status: "0",
  },
  queryParams: {
    page: 1,
    limit: 10,
    name: undefined,
    status: undefined,
  },
  rules: {
    userId: [{ required: true, message: "请选择客服姓名", trigger: "change" }],
    phone: [
      { required: true, message: "请输入正确手机号", trigger: "blur" },
      {
        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
        message: "请输入正确的手机号码",
        trigger: "blur",
      },
    ],
    sex: [{ required: true, message: "请选择性别", trigger: "change" }],
    status: [{ required: true, message: "请选择状态", trigger: "change" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 提交按钮 */
const submitFormDebounceHandler = debounce(submitForm, 500);
function submitForm() {
  proxy.$refs["CustomerServiceAgentsRef"].validate((valid) => {
    if (valid) {
      agentSave(form.value).then((res) => {
        console.log("222222222", res);
        proxy.$modal.msgSuccess("新增成功");
        cancel();
        getList();
      })
    }
  });
}
function add() {
  open.value = true;
}
function del(data) {
  proxy.$modal
    .confirm("是否确认删除该客服？")
    .then(function () {
      return agentDelete([data.id])
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
function changePeople(val) {
  form.value.userName = sysPepoleList.value.find(
    (item) => item.userId == val,
  )?.userName

  form.value.nickName = sysPepoleList.value.find(
    (item) => item.userId == val,
  )?.nickName
}
function stopUse(data) {
  proxy.$modal
    .confirm("是否确认停用/启用该客服？")
    .then(function () {
      let status
      if (data.status == '1') {
        status = '0';
      } else {
        status = '1';
      }
      console.log('2222222222222222222222222222', status)
      return isEnableStatus({ id: data.id, status: status })
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("操作成功");
    })
    .catch(() => {});
}
function cancel() {
  open.value = false;
  reset();
}
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    status: "0",
    sex: undefined,
    phone: undefined,
  };

  proxy.resetForm("CustomerServiceAgentsRef");
}

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  agentList(queryParams.value).then((response) => {
    postList.value = response.page.list;
    total.value = response.page.totalCount;
    loading.value = false;
  })
}
function handleQuery() {
  getList();
}
function SysUserSelectOptions() {
  getSysUserSelectOptions().then((res) => {
    sysPepoleList.value = res.data
  });
}
function resetQuery() {
  queryParams.value = {
    page: 1,
    limit: 10,
    name: undefined,
    status: undefined,
  };
  getList();
}
getList();
SysUserSelectOptions()
</script>
